package com.his.menu.controller;

import com.his.common.verification.group.Update;
import com.his.config.annotation.CurrentUser;
import com.his.login.entity.DictEmployee;
import com.his.menu.service.MenuService;
import com.his.menu.vo.MenuVo;
import com.xiao.common.controller.ResultBaseController;
import com.xiao.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author xiaoxiaoping
 * @Date 2020/1/21
 */
@RestController
@RequestMapping(value="/api/menu")
public class MenuController extends ResultBaseController {

    private  final org.slf4j.Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());
    @Resource
    private MenuService menuService;

    @PostMapping(value="/insert.do")
    public ResultUtil insertMenu(@RequestBody @Validated MenuVo menuVo,@CurrentUser DictEmployee dictEmployee){
        try {
            menuService.insert(menuVo);
            return this.success("保存成功！", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @PostMapping(value="/query_all.do")
    public ResultUtil getMenu(@CurrentUser DictEmployee dictEmployee, MenuVo menuVo){
        try{
            return this.success("",menuService.getAllMenu(menuVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }

    }
    @PostMapping(value="/get_menu_list.do")
    public ResultUtil getMenuList(){
        try{
            return this.success("",menuService.getMenuList());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @PostMapping(value="/get_menu_by_emp.do")
    public ResultUtil getMenuListByEmp(@CurrentUser DictEmployee dictEmployee){
        try{
            return this.success("",menuService.getMenuListByEmp(dictEmployee));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @PostMapping(value = "/update.do")
    public ResultUtil updateById(@Validated({Update.class}) @RequestBody MenuVo menuVo){
        try{
            menuService.updateById(menuVo);
            return this.success("修改成功！",null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @GetMapping(value = "/get_menu_by_role.do")
    public ResultUtil getMenuByRole(@CurrentUser DictEmployee dictEmployee, String roleId){
        try{
            return this.success("", menuService.getMenuListByRoleId(roleId));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
}
